/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;

/**
 *
 * @author 
 * La genera le due chiavi RSA che andranno preventivamente scambiate in sicurezza fra client e server.
 * Le chiavi vengono salvate in chiaro sui relativi file di testo in base 16
 */
public class RsaKeyGenerator {

    public static void main(String[] args) throws NoSuchAlgorithmException, FileNotFoundException, IOException, KeyStoreException, CertificateException, InvalidKeySpecException {
        KeyPairGenerator gen1 = null;
        KeyPairGenerator gen2 = null;

        // RSA
        gen1 = KeyPairGenerator.getInstance("RSA");
        gen2 = KeyPairGenerator.getInstance("RSA");

        // Chiavi modulo 512 bit, seme definito da Java
        gen1.initialize(1024, new SecureRandom());
        gen2.initialize(1024, new SecureRandom());

        // coppia di chiavi pubblica/privata
        KeyPair kp = gen1.generateKeyPair();
        // recupera le due chiavi, pubblica e privata
        RSAPrivateKey kprivServer = (RSAPrivateKey) kp.getPrivate();
        RSAPublicKey kpubServer = (RSAPublicKey) kp.getPublic();

        kp = gen2.generateKeyPair();
        RSAPrivateKey kprivClient = (RSAPrivateKey) kp.getPrivate();
        RSAPublicKey kpubClient = (RSAPublicKey) kp.getPublic();

        //Scrivo un file di testo contenente modulo e esponente privato
        FileWriter fw = new FileWriter("sPrivate.key");
        fw.write(kprivServer.getModulus().toString(16).toUpperCase());
        fw.write("\n");
        fw.write(kprivServer.getPrivateExponent().toString(16).toUpperCase());
        fw.close();
        
        fw = new FileWriter("cPrivate.key");
        fw.write(kprivClient.getModulus().toString(16).toUpperCase());
        fw.write("\n");
        fw.write(kprivClient.getPrivateExponent().toString(16).toUpperCase());
        fw.close();
        
        fw = new FileWriter("sPublic.key");
        fw.write(kpubServer.getModulus().toString(16).toUpperCase());
        fw.write("\n");
        fw.write(kpubServer.getPublicExponent().toString(16).toUpperCase());
        fw.close();
        
        fw = new FileWriter("cPublic.key");
        fw.write(kpubClient.getModulus().toString(16).toUpperCase());
        fw.write("\n");
        fw.write(kpubClient.getPublicExponent().toString(16).toUpperCase());
        fw.close();
    }
}
